<template>
    <div class="table-page">
        <div class="table-search-box" @keyup.enter="search()">
            <div class="search-item">
                <label>大类型：</label>
                <div class="table-query-input">
                    <el-select
                        v-model="queryData.firstType"
                        clearable
                        placeholder="请选择"
                    >
                        <el-option
                            v-for="item in options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        >
                        </el-option>
                    </el-select>
                </div>
                <label>题型名称：</label>
                <div class="table-query-input">
                    <el-input
                        v-model="queryData.name"
                        clearable
                        placeholder="试题名称"
                    ></el-input>
                </div>
                <label>题型备注：</label>
                <div class="table-query-input">
                    <el-input
                        v-model="queryData.remark"
                        clearable
                        placeholder="试题备注"
                    ></el-input>
                </div>
            </div>
            <div class="search-item">
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    :loading="tableLoading"
                    @click="clickSearch()"
                    >搜索
                </el-button>
            </div>
            <div class="search-item">
                <el-button
                    icon="el-icon-refresh"
                    :loading="tableLoading"
                    @click="resetSearch()"
                >重置
                </el-button>
            </div>
        </div>
        <div class="table-operation-box">
            <el-button
                type="primary"
                icon="el-icon-plus"
                style="margin-bottom: 2em"
                @click="clickAdd('添加题型')"
                >新增题型
            </el-button>
<!--            <el-button
                type="danger"
                icon="el-icon-delete"
                style="margin-bottom: 2em"
                :disabled="ids.length === 0"
                @click="
                    tableDelete(
                        '/exam/question-type/deleted-list',
                        {
                            ids: ids
                        },
                        true
                    )
                "
                >批量删除
            </el-button>-->
        </div>
        <div class="table-data-box">
            <el-table
                ref="table"
                v-loading="tableLoading"
                border
                :data="tableData"
                style="width: 100%;min-height:40em"
                row-key="id"
                :highlight-current-row="true"
                @selection-change="handleSelectionChange"
            >
                <el-table-column
                    type="selection"
                    min-width="55"
                    align="center"
                    :resizable="false"
                >
                </el-table-column>
                <el-table-column
                    prop="name"
                    label="题型别名"
                    width="150"
                    align="center"
                    :resizable="false"
                >
                </el-table-column>
                <el-table-column
                    prop="firstType"
                    label="题型类型"
                    width="100"
                    align="center"
                    :resizable="false"
                >
                    <template slot-scope="scope">
                        {{ status[scope.row.firstType] }}
                    </template>
                </el-table-column>
                <el-table-column
                    prop="remark"
                    label="题型备注"
                    align="center"
                    :resizable="false"
                >
                </el-table-column>
                <el-table-column
                    prop="createdTime"
                    label="创建日期"
                    align="center"
                    :resizable="false"
                >
                </el-table-column>
                <el-table-column
                    label="最后操作者"
                    align="center"
                    :resizable="false"
                >
                    <template slot-scope="scope">
                        <div>
                            <p>{{ scope.row.finalModifyUserName }}</p>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    width="200"
                    align="center"
                    fixed="right"
                    :resizable="false"
                >
                    <template slot-scope="scope">
                        <el-button
                            class="primary"
                            type="text"
                            size="mini"
                            icon="el-icon-edit"
                            :loading="scope.row.opLoading"
                            @click="clickSimpleEdit(scope.row, '试题类型编辑')"
                            >编辑
                        </el-button>
                        <el-button
                            v-if="!scope.row.edit"
                            class="danger"
                            type="text"
                            icon="el-icon-delete"
                            size="mini"
                            :loading="scope.row.opLoading"
                            @click="
                                tableDelete(
                                    '/exam/question-type/deleted-list',
                                    {
                                        ids: [scope.row.id]
                                    },
                                    true
                                )
                            "
                            >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div v-if="pageData.total" class="xy-between table-page-box">
            <el-pagination
                :current-page.sync="pageData.currentPage"
                :page-sizes="pageData.pageSizes"
                :page-size.sync="pageData.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageData.total"
            >
            </el-pagination>
        </div>
        <el-dialog
            :title="dialogFormTitle"
            :visible.sync="dialogFormVisible"
            width="20%"
        >
            <el-form
                ref="dataForm"
                :model="dataForm"
                label-width="8em"
                :rules="dataFormRule"
                class="table-edit-from"
            >
                <el-input v-model="dataForm.id" type="hidden"></el-input>
                <el-form-item label="大类型" prop="firstType">
                    <el-select
                        v-model="dataForm.firstType"
                        :disabled="dataForm.firstType !== null"
                        placeholder="请选择"
                    >
                        <el-option
                            v-for="item in options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="题型名称" prop="name">
                    <el-input v-model="dataForm.name"></el-input>
                </el-form-item>
                <el-form-item label="题型备注" prop="remark">
                    <el-input v-model="dataForm.remark"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button :loading="submitFlag" @click="dialogClose"
                    >取 消</el-button
                >
                <el-button
                    :loading="submitFlag"
                    type="primary"
                    @click="formDataSave('/exam/question-type/update')"
                    >保 存</el-button
                >
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { TablePage } from "@/layout/mixin/iVue.js";
import { mapState } from "vuex";

export default {
    name: "QuestionTypeIndex",
    components: {},
    mixins: [TablePage],
    data() {
        return {
            options: [
                {
                    value: "singleChoice",
                    label: "单选题"
                },
                {
                    value: "multipleChoice",
                    label: "多选题"
                },
                {
                    value: "judgment",
                    label: "判断题"
                },
                {
                    value: "fill",
                    label: "填空题"
                },
                {
                    value: "text",
                    label: "问答题"
                },
                {
                    value: "combination",
                    label: "复合题"
                }
            ],
            queryData: {
                name: "",
                remark: "",
                firstType: "",
                pageSize: 10,
                currentPage: 1
            },
            tableData: [],
            ids: [],
            initAddForm: {
                id: null,
                name: null,
                remark: null,
                firstType: null
            },
            multipleSelection: [],
            dataFormRule: {
                firstType: [
                    {
                        required: true,
                        message: "请选择大类型！",
                        trigger: "change"
                    }
                ],
                name: [
                    {
                        required: true,
                        message: "题型名称不可为空！",
                        trigger: "blur"
                    }
                ]
            }
        };
    },
    computed: {
        ...mapState({
            status: state => state.status.questionFirstType
        })
    },
    watch: {
        actionSearch(val) {
            this.$refs.tree.filter(val);
        }
    },
    methods: {
        clearQuery() {
            (this.queryData = {}), this.search();
        },
        search(queryData) {
            this.loadTable("/exam/question-type/page", queryData);
        },
        handleSelectionChange(val) {
            this.ids = [];
            const length = val.length;
            for (let n = 0; n < length; n++) {
                ids.push(val[n].id);
            }
        },
        filterAction(value, data) {
            if (!value) return true;
            return data.label.indexOf(value) !== -1;
        },
        clickSelectAction(data, checked, indeterminate) {
            const actionCode = data.id;
            if (checked) {
                this.dataForm.actionCodes.push(actionCode);
            } else {
                this.dataForm.actionCodes.splice(
                    this.dataForm.actionCodes.indexOf(actionCode),
                    1
                );
            }
        }
    }
};
</script>

<style lang="scss" scoped>
.search-item{
    label{
        margin-left: 1em;
    }
}
</style>
